package cn.stylefeng.guns.modular.school.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.school.entity.ClassMsg;
import cn.stylefeng.guns.modular.school.mapper.ClassMsgMapper;
import cn.stylefeng.guns.modular.school.model.params.ClassMsgParam;
import cn.stylefeng.guns.modular.school.model.params.ClassMsgStudentParam;
import cn.stylefeng.guns.modular.school.model.params.StudentInfoParam;
import cn.stylefeng.guns.modular.school.model.result.ClassMsgResult;
import cn.stylefeng.guns.modular.school.model.result.StudentInfoResult;
import cn.stylefeng.guns.modular.school.service.ClassMsgService;
import cn.stylefeng.guns.modular.school.service.ClassMsgStudentService;
import cn.stylefeng.guns.modular.school.service.StudentInfoService;
import cn.stylefeng.guns.sys.core.shiro.ShiroKit;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 班级通知 服务实现类
 * </p>
 *
 * @author
 * @since 2019-12-01
 */
@Service
public class ClassMsgServiceImpl extends ServiceImpl<ClassMsgMapper, ClassMsg> implements ClassMsgService {
    @Autowired
    private StudentInfoService studentInfoService;
    @Autowired
    private ClassMsgStudentService classMsgStudentService;

    @Override
    @Transactional(rollbackOn = Exception.class)
    public void add(ClassMsgParam param) {
        for (Integer classId : param.getClassIds()) {
            ClassMsg entity = getEntity(param);
            entity.setTopFlag(ToolUtil.isEmpty(param.getTopFlag()) ? "1" : "0");
            entity.setDisplayFlag(ToolUtil.isEmpty(param.getDisplayFlag()) ? "1" : "0");
            entity.setClassId(classId);
            entity.setDelFlag(false);
            entity.setCreateUser(ShiroKit.getUser().getId());
            entity.setCreateTime(new Date());
            entity.setUpdateTime(new Date());
            entity.setUpdateUser(ShiroKit.getUser().getId());
            this.save(entity);

            // 给当前班级的所有学生发送通知。
            StudentInfoParam studentInfoParam = new StudentInfoParam();
            studentInfoParam.setClassId(classId);
            List<StudentInfoResult> studentInfoResults = studentInfoService.findListBySpec(studentInfoParam);
            studentInfoResults.forEach(studentInfoResult -> {
                ClassMsgStudentParam classMsgStudentParam = new ClassMsgStudentParam();
                classMsgStudentParam.setClassMsgId(entity.getId());
                classMsgStudentParam.setStudentId(studentInfoResult.getId());
                classMsgStudentParam.setDelFlag(false);
                classMsgStudentParam.setCreateTime(new Date());
                classMsgStudentParam.setCreateUser(ShiroKit.getUser().getId());
                classMsgStudentService.add(classMsgStudentParam);
            });
        }

    }

    @Override
    public void delete(ClassMsgParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(ClassMsgParam param) {
        ClassMsg oldEntity = getOldEntity(param);
        ClassMsg newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        if (ToolUtil.isEmpty(newEntity.getDisplayFlag())) {
            newEntity.setDisplayFlag("1");
        }
        if (ToolUtil.isEmpty(newEntity.getTopFlag())) {
            newEntity.setTopFlag("1");
        }
        this.updateById(newEntity);
    }

    @Override
    public ClassMsgResult findBySpec(ClassMsgParam param) {
        return null;
    }

    @Override
    public List<ClassMsgResult> findListBySpec(ClassMsgParam param) {
        return this.baseMapper.customList(param);
    }

    @Override
    public List<ClassMsgResult> findListByClassIds(ClassMsgParam param) {
        return this.baseMapper.customListByClassIds(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(ClassMsgParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public ClassMsg findTopOne(int classId) {
        QueryWrapper<ClassMsg> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("class_id", classId).eq("top_flag", "0").eq("display_flag", "0").orderByDesc("create_time").last("limit 0 , 1");
        return this.baseMapper.selectOne(queryWrapper);
    }

    private Serializable getKey(ClassMsgParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ClassMsg getOldEntity(ClassMsgParam param) {
        return this.getById(getKey(param));
    }

    private ClassMsg getEntity(ClassMsgParam param) {
        ClassMsg entity = new ClassMsg();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
